package com.x.epspace.common;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * <p>Title: 日期处理类</p>
 * <p>Description: 日期处理类</p>
 * @version $Revision: 1.3 $ $Date: 2006/10/27 11:46:35 $
 */
public class DateTools
{

    // the following const is to define date format.
    public static final int FMT_DATE_YYYYMMDD = 1;
    public static final int FMT_DATE_YYYYMMDD_HHMMSS = 2;
    public static final int FMT_DATE_HHMMSS = 3;
    public static final int FMT_DATE_HHMM = 4;
    public static final int YYYYMMDD = 5;


    /**
     * 将特定格式的字符串转换成java.util.Date对象<p>
     * 
     * @param strDate a date string
     * @param nFmtDate specific date string format defined in this class.
     * @return Date
     * @throws Exception
     */
    public static Date parseDate(String strDate, int nFmtDate) throws Exception
    {
        SimpleDateFormat fmtDate = new SimpleDateFormat();
        switch (nFmtDate) {
            default:
            case DateTools.FMT_DATE_YYYYMMDD:
                fmtDate.applyPattern("yyyy-MM-dd");
                break;
            case DateTools.FMT_DATE_YYYYMMDD_HHMMSS:
                fmtDate.applyPattern("yyyy-MM-dd HH:mm:ss");
                break;
            case DateTools.FMT_DATE_HHMM:
                fmtDate.applyPattern("HH:mm");
                break;
            case DateTools.FMT_DATE_HHMMSS:
                fmtDate.applyPattern("HH:mm:ss");
                break;
            case DateTools.YYYYMMDD:
            	fmtDate.applyPattern("yyyyMMdd");
            	break;
        }
        return fmtDate.parse(strDate);
    }

    /**
     * <p> 将两个日期进行比较，获取其相差的年份</p>
     * 
     * @param compDate1  ：指定的日期1
     * @param compDate2 ：指定的日期2
     * @return 相差的年份
     */
    public static long getAppoinDate(java.util.Date compDate1,java.util.Date compDate2)
    {
        if( compDate1 == null || compDate2 == null){            
            return -1;
        }

        long nYear = (compDate1.getTime() - compDate2.getTime()) / (24 * 3600 * 1000) / 365;

        return nYear;
    }

    /**
     * 
     * <p> 比较时间应该在form.tmBegin.value,form.tmEnd.value之前 </p>
     * 
     * @param tmBegin
     * @param tmEnd
     * @param tmAlert
     * @return boolean
     */
    public static boolean compareTime(Date tmBegin, Date tmEnd, Date tmAlert)
    {
        boolean bReturn = false; 
        long lBegin = tmBegin.getTime();
        long lEnd = tmEnd.getTime();
        long lAlert = tmAlert.getTime();

        if (((lBegin - lAlert) < 0) && ((lAlert - lEnd) < 0)) {
            bReturn =  true;
        }
        return bReturn;
    }
    
    /**
     * 
     * <p> 比较时间tmBegin和tmEnd大小 </p>
     * 
     * @param tmBegin
     * @param tmEnd
     * @return boolean
     */
    public static boolean compareTime(Date tmBegin, Date tmEnd)
    {
        boolean bReturn = false; 
        long lBegin = tmBegin.getTime();
        long lEnd = tmEnd.getTime();

        if (lBegin < lEnd) {
            bReturn =  true;
        }
        return bReturn;
    }

    /**
     * 日期格式的转换<p>
     *
     * @param ts Timestamp格式的时间
     * @return 日期字符串
     */
    public static String getDispalyDate(Timestamp ts)
    {
        if (ts == null) {
            return "";
        }
        return MessageFormat.format("{0,time,yyyy-MM-dd}", new Object[] {ts});
    }

    /**
     * 显示当前日期
     * 
     * @return 日期字符串
     */
    public static String getDisplayDate() {
        return MessageFormat.format("{0,time,yyyy-MM-dd}", new Object[] {DateTools.getCurrentTime()});
    }

    /**
     * 将日期字符串转成timestamp<p>
     * 
     * @param strDate 日期字符串
     * @return timestamp类型格式的时间
     */
    public static Timestamp getTimestampTime(String strDate)
    {
    	if (strDate.length() == 10) {
    		strDate = strDate + " 00:00:00";
    	}
    	if (strDate.length() == 16) {
    		strDate = strDate + ":00";
    	}
        return getTimestamp(strDate, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将日期字符串转成timestamp<p>
     * 
     * @param strDate 日期字符串
     * @return timestamp类型格式的时间
     */
    public static Timestamp getTimestamp(String strDate)
    {
        return getTimestamp(strDate, "yyyy-MM-dd");
    }


    /**
     * 将日期字符串转成timestamp<p>
     * 
     * @param strDate 日期字符串
     * @return timestamp类型格式的时间
     */
    public static Timestamp getTimestamp(String strDate, String strFormat)
    {
        if (strDate == null || strDate.equals("")) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
        Date date = null;

        try {
            date = sdf.parse(strDate);
        } catch (ParseException pe) {
            throw new RuntimeException("错误的日期字符串！");
        }

        return new Timestamp(date.getTime());
    }

    /**
     * 显示时间<p>
     *
     * @param ts 时间
     * @return 时间字符串
     */
    public static String getDisplayTime(Timestamp ts) {
        if (ts == null) {
            return "";
        }
        return MessageFormat.format("{0,time,yyyy-MM-dd HH:mm:ss}", new Object[] {ts});
    }


    /**
     * 显示当前时间<p>
     * 
     * @return 时间字符串
     */
    public static String getDisplayTime() {
        return MessageFormat.format("{0,time,yyyy-MM-dd HH:mm:ss}", new Object[] {DateTools.getCurrentTime()});
    }


    /**
     * 获取当前时间<p>
     *
     * @return 当前时间
     */
    public static Timestamp getCurrentTime() {
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * 在字串前面增加字符到指定长度<p>
     *
     * @param str 字符串
     * @param c 字符
     * @param len 长度
     * @return 定长字符串
     */
    public static String getFixedString(String str, char c, int len) {

        String strResult = "";

        for (int i = 0; i < len - str.length(); i++) {
            strResult += c;
        }

        return strResult + str;
    }
    
    /**
     * 获取当前年份字符串
     * @return
     */
    public static String getCurrentYear(){
    	Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		return String.valueOf(year);
    }
    
    /**
     * 获取当前月份字符串
     * @return
     */
    public static String getCurrentMonth(){
    	Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH)+1;
		return StringTools.len(String.valueOf(month), 2);
    }
    
    public static  String formateDate(java.sql.Date date){
    	DateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	return dFormat.format(date);
    }
    
    public static  String formateDate(java.util.Date date){
    	DateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    	return dFormat.format(date);
    }
    
    /** 
     * 将微信消息中的CreateTime转换成标准格式的时间（yyyy-MM-dd HH:mm:ss） 
     *  
     * @param createTime 消息创建时间 
     * @return 
     */  
    public static String formatTime(String createTime) {  
        // 将微信传入的CreateTime转换成long类型，再乘以1000  
        long msgCreateTime = Long.parseLong(createTime) * 1000L;  
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        return format.format(new Date(msgCreateTime));  
    }
    
    public static long getCreateTime(){
    	return System.currentTimeMillis();
    }
    
    public static long getMonthFirstDate(long day){
    	SimpleDateFormat sdfYear=new SimpleDateFormat("yyyy");  
		SimpleDateFormat sdfMonth=new SimpleDateFormat("MM");
		if(day<1000000000000l) day = day*1000;
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,Integer.parseInt(sdfYear.format(day)));
		cal.set(Calendar.MONTH,Integer.parseInt(sdfMonth.format(day)));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.AM_PM, 0);
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		Date firstDate = cal.getTime();
		return firstDate.getTime();
    }
    
    public static long getMonthLastDate(long day){
    	SimpleDateFormat sdfYear=new SimpleDateFormat("yyyy");  
		SimpleDateFormat sdfMonth=new SimpleDateFormat("MM");
		if(day<1000000000000l) day = day*1000;
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR,Integer.parseInt(sdfYear.format(day)));
		cal.set(Calendar.MONTH,Integer.parseInt(sdfMonth.format(day)));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.AM_PM, 1);
		cal.set(Calendar.HOUR, 11);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		Date lastDate = cal.getTime();
		return lastDate.getTime();
    }
    
    /**
     * 获取微信支付订单日期
     */
    public static String getDatetime(int i){
    	Date date=new Date();//取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE,i);//把日期往后增加.整数往后推,负数往前移动
		date=calendar.getTime(); //这个时间就是日期往后推的结果 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		return formatter.format(date);
    }
    
    public static void main(String[] args) {
    	//System.out.println(getDatetime(0));
    	try {
			System.out.println(parseDate("2015-01-01 23:12:23",2));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
        
}
